package org.hengguan.api.contest.config;

import com.github.xiaoymin.knife4j.spring.extension.OpenApiExtensionResolver;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.util.Collections;

@EnableSwagger2WebMvc
@Import(BeanValidatorPluginsConfiguration.class)
@Configuration
public class Swagger2Config {

	@Autowired
	OpenApiExtensionResolver openApiExtensionResolver;
	@Value("${server.port}")
	private String serverPort;
	@Value("${server.servlet.context-path}")
	private String contextPath;
	@Value("${spring.application.name}")
	private String applicationName;
	@Bean
	public Docket docket() {
		String groupName = "CONTEST";
		return new Docket(DocumentationType.SWAGGER_2)
						.extensions(openApiExtensionResolver.buildExtensions(groupName))
						.select()
							.apis(RequestHandlerSelectors.basePackage("org.hengguan.api.contest.module"))
							.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
							.paths(PathSelectors.any())
							.build()
						.groupName(groupName)
						.securitySchemes(Collections.singletonList(securityScheme()))
						.securityContexts(Collections.singletonList(securityContext()))
						.apiInfo(new ApiInfoBuilder()
								.termsOfServiceUrl("localhost:"+serverPort+contextPath+"/doc.html")
								.description("HENGGUAN-API-MEDIA文档")
								.version("v1.0")
								.contact(new Contact("", "", ""))
								.title("HENGGUAN-API接口文档")
								.build())
//						.globalOperationParameters(Collections.singletonList(new ParameterBuilder()
//													.name("Authorization")
//													.description("access_token")
//													.modelRef(new ModelRef("string"))
//													.parameterType("header")
//													.defaultValue("bearer ")
//													.required(false)
//													.build()))
						;
	}

	ApiKey apiKey() {
		return new ApiKey("Token", "Authorization", "header");
	}
//	@Bean
	SecurityScheme securityScheme() {
		return apiKey();
	}
	//	@Bean
//	public SecurityScheme securityScheme() {
//		return new OAuthBuilder()
//				.name(applicationName)
//				.grantTypes(Collections.singletonList(new ResourceOwnerPasswordCredentialsGrant("http://localhost:8180/dynamic-control-auth/oauth/token")))
//				.scopes(Collections.singletonList(new AuthorizationScope("web", applicationName+" scope")))
//				.build();
//	}
//	@Bean
	public SecurityContext securityContext() {
		return SecurityContext.builder()
				.securityReferences(Collections.singletonList(new SecurityReference(apiKey().getName(), new AuthorizationScope[] {new AuthorizationScope("contest", "CONTEST域")})))
				.forPaths(PathSelectors.any())
				.build();
	}
}
